/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.controller;

import com.example.domain.MailRequest;
import com.example.domain.User;
import com.example.utils.RandomChineseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
@CrossOrigin
public class BasicController {

    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private SimpleMailMessage mail;

    @PostMapping("/sendMail")
    public Boolean sendMail(@RequestBody MailRequest mailRequest) {
        mail.setSubject(mailRequest.getSubject());
        mail.setText(mailRequest.getContent());
        javaMailSender.send(mail);
        System.out.println("Email发送完成");
        return true;
    }
    @GetMapping("/hello")
    public String hello(){
        System.out.println("Hello world !");
        List<User> users = new ArrayList<>();
        for (int i = 0;i < 10; i++){
            User user = new User();
            user.setId((long) i);
            user.setName(RandomChineseUtils.getRandomChineseString(2,3));
            user.setPhone(String.valueOf(ThreadLocalRandom.current().nextInt(10,1000)));
            user.setAge(i);
            user.setEmail(UUID.randomUUID().toString());
            user.setAddress(RandomChineseUtils.getRandomChineseString(10));
            users.add(user);
        }
        return "hello world ! " + users;
    }
    @GetMapping("/get")
    public String get(){
        return "boy";
    }
    @GetMapping("/find")
    public String find(){
        // 进入find方法
        System.out.println("Entering find method");
        // 打印dl;fkjl;dsjfl;djsl;jf
        System.out.println("dl;fkjl;dsjfl;djsl;jf");
        // 退出find方法
        System.out.println("Exiting find method");
        // 返回girl
        return "girl";
    }

}
